package net.xuele.learn.mq;


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Random;

@Slf4j
@EnableScheduling
@Component
public class SendMessage {

    @Autowired
    private MessageProducer messageProducer;

    @Scheduled(cron = "0 21 13 * * ?")
    public void sendMessage() {
        log.info("=======start send msg======");

        int number = new Random().nextInt(10) + 1;

        if (number % 2 == 0) {
            // 发送补登消息
            ClaimRecord claimRecord = new ClaimRecord();
            claimRecord.setLineNo("5");
            claimRecord.setStationNo("五号线");
            claimRecord.setTripSeq("YUEUYEWH124457EUIEDGGF");

            byte[] body = JSON.toJSONString(claimRecord).getBytes();
            String keys = "claim msg";
            messageProducer.sendMessage(keys, body, Constant.CLAIM_TAG);
            log.info("=======already send claim message=====, {}",claimRecord);
        } else {
            // 发送补登确认消息

            ClaimAck claimAck = new ClaimAck();
            claimAck.setClaimTIme(new Date());
            claimAck.setCount(number);
            claimAck.setLineNo("5");

            byte[] body = JSON.toJSONString(claimAck).getBytes();
            String keys = "claim msg";
            messageProducer.sendMessage(keys, body, Constant.ACK_TAG);
            log.info("=======already send ack message=====, {}",claimAck);
        }
    }
}
